package com.nwpu.bigdata.service.impl;

import java.io.IOException;

import java.util.ArrayList;
import java.util.List;
import org.influxdb.InfluxDB;
import org.influxdb.InfluxDBFactory;
import org.influxdb.dto.QueryResult;
import org.influxdb.dto.QueryResult.Result;
import org.influxdb.dto.QueryResult.Series;
import org.springframework.beans.BeanWrapperImpl;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.ImportResource;
import org.springframework.stereotype.Service;

import com.nwpu.bigdata.dto.Cassandra;
import com.nwpu.bigdata.service.CassandraService;

import com.nwpu.bigdata.util.impl.InfluxdbService;


/**
 *@version:2018年4月25日下午8:45:51
*/
/**
 * @author Administrator
 *
 */
@Service
@ImportResource(locations= {"classpath:/xmlConfig/spring/applicationContext-util.xml"})
public class CassandraServiceImpl implements CassandraService{
	/*@Autowired
	private InfluxDBUtilImpl influDBClient;*/
	
	@Value("${retentionPolicy.cassandra}")
	private String retentionPolicy;
	
	@Value("${influxdb.agentDB}")
	private String dbname;
	@Value("${influxdb.url}")
	private String url;
	@Value("${influxdb.user}")
	private String usrname;
	@Value("${influxdb.password}")
	private String password;
	
//从数据库中获取cassandra的指标信息
public List<Cassandra> cassandraCol() throws IOException{
	


	InfluxDB influxDB = InfluxDBFactory.connect(url, usrname, password);//建立influxdb连接
    InfluxdbService service = new InfluxdbService(dbname,retentionPolicy,influxDB);
    if(service.equals(null))
    {
    	
    	return null;
    }
    
    QueryResult queryResult = service.query("SELECT * FROM \"cassandra\" WHERE time > now() - 10m ORDER BY time DESC");//查询表中最近10分钟的数据
    System.out.println(queryResult);
   // List<QueryResult.Result> results = queryResult.getResults();//将查询结果放入list中
    
    if(queryResult.getResults() == null) {
		System.out.println("the result is empty, end query!");
		return null; 
	}
	
	List<Cassandra> lists = new ArrayList<Cassandra>();
	//遍历查询结果获取查询结果中有用的信息
	for(Result result : queryResult.getResults()){
		List<Series> series = result.getSeries();
		System.out.println("^^^^^^"+result.getSeries());
		for(Series serie : series) {
			
			List<List<Object>> values = serie.getValues();
			List<String> columns = serie.getColumns();
			
			System.out.println(serie.getName() + "  " + serie.getTags());
			
			System.out.println(columns);
			
			System.out.println(values);
			
			lists.addAll(getQueryData(columns, values));

			/*for(MetricInfo metricInfo : lists) {
				System.out.println(metricInfo);
			}
			*/
		}
		
	}
   
    return lists;
}

//提取查询结果中的有用信息
public  List<Cassandra> getQueryData(List<String> columns, List<List<Object>>  values){  
    List<Cassandra> lists = new ArrayList<>();  
      
    for (List<Object> list : values) {  
    	Cassandra info = new Cassandra();
    	BeanWrapperImpl bean = new BeanWrapperImpl(info);
        for(int i=0; i< list.size(); i++){  
//          String propertyName = setColumns(columns.get(i));
            String propertyName = columns.get(i);
            Object value = list.get(i); 
            if(value == null){
            	 bean.setPropertyValue(propertyName, 0);  
            }
            else{
            bean.setPropertyValue(propertyName, value);  
            }
        }  
          
        lists.add(info);  
    }  
      
    return lists;  
}

}
